<template>
    <div>
      <el-form :model="ruleForm" size="medium" :rules="rules" label-position="top" label-width="80px" ref="accountSettingForm">
        <!--<h3 class="login_title">login</h3>-->
        <h3>更新个人信息</h3>
        <el-form-item label="昵称" prop="nickname">
          <el-input type="text" v-model="ruleForm.nickname" auto-complete="off" placeholder="昵称"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="text" v-model="ruleForm.email" auto-complete="off" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input type="text" v-model="ruleForm.phoneNumber" auto-complete="off" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input type="text" v-model="ruleForm.age" auto-complete="off" placeholder="年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="ruleForm.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="个人简介" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc" auto-complete="off" placeholder="个人简介"></el-input>
        </el-form-item>
        <el-form-item style="width: 30%">
          <el-button type="primary"  @click="submitForm()" style="width: 100%">提交更新</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
export default {
  name: 'AccountSetting',
  data () {
    return {
      ruleForm: {
        id: '',
        nickname: '',
        email: '',
        phoneNumber: '',
        age: '',
        gender: '',
        avatar: '',
        desc: ''
      },
      rules: {
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'change' },
          { min: 2, max: 20, message: '长度在2到20之间', trigger: 'change' }
        ],
        email: [
          { type: 'email', required: true, message: '请输入格式正确的邮箱', trigger: 'change' }
        ],
        age: [
          { required: true, message: '请选择年龄', trigger: 'change' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.accountSettingForm.validate((valid) => {
        if (valid) {
          // const userId = this.$store.getters.getCurrentUserId
          // console.log('userId(accountSetting):' + userId)

          this.$axios.post('/users/account_update', this.ruleForm, {
            headers: { Authorization: this.$store.state.jwt }
          }).then(response => {
            this.ruleForm = response.data.data
          })
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    // 获取用户信息填充到表单
    getAccountInfo () {
      const userInfo = this.$store.getters.getUserInfo
      const userId = userInfo.id
      // console.log('userId(getAccountInfo):' + userId)
      this.$axios.get('/users/' + userId, {
        headers: { Authorization: this.$store.state.jwt }
      }).then(response => {
        this.ruleForm = response.data.data
      })
    }
  },
  mounted () {
    this.getAccountInfo()
  }
}
</script>

<style scoped>
  /*logo图片样式*/
  .m-logo img{
    width: 68px;
    padding-left: 133px;
    padding-bottom: 20px;
  }
  .el-form-item {
    width: 60%;
  }
</style>
